package com.coin900.coin.rabbitmq.receiver;

import com.coin900.coin.persist.fu.FuCapitalTransactionVO;
import com.coin900.coin.service.fu.IFuCapitalTransactionService;
import com.value.utils.StringUtil;
import net.sf.json.JSONObject;
import org.apache.commons.lang.StringUtils;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.*;

/**
 * 消息接受者
 * <p>
 * 接收币新充值情况的消息，返回值为 txid
 * 收到 txid 之后调用 /gettransaction 来判断转帐是否成功
 * /gettransaction?currency=xxx&txid=xxx
 * currency 现在传 'eth'
 *
 * @author shui
 * @return { isSuccess: true/false, details: {confirmations: 23, from:, to, value:} }
 * <p>
 * 1.成功 isSuccess-true：
 * 将返回的信息用户充值成功
 * <p>
 * 2.失败 isSuccess-false:
 * 继续确认，一小时之后任然失败则表示交易失败
 */
@Component
public class Receiver {

    @RabbitHandler
    @RabbitListener(queues = "aQueue")
    public void process(String message) {
        if (message != null) {
            message = this.decodeMessage(message);
            this.handleMessage(message);
        } else {
            return;
        }
    }

    /**
     * 消息格式的处理
     *
     * @param message
     * @return
     */
    private String decodeMessage(String message) {
        String[] split = message.split(",");
        StringBuilder builder = new StringBuilder();
        for (int i = 0; i < split.length; i++) {
            builder.append((char) Integer.parseInt(split[i]));
        }

        message = builder.toString();
        message = message.replace("\\", "");
        message = message.substring(1, message.length() - 1);
        return message;
    }

    /**
     * 处理消息
     * 获取 txid，插入到
     *
     * @param message
     */
    private void handleMessage(String message) {
        // 获取 txid
        JSONObject jsonObject = JSONObject.fromObject(message);
        String txid = jsonObject.getString("hash");
        // 把 txid 存起来
        FuCapitalTransactionVO fuCapitalTransactionVO = new FuCapitalTransactionVO();
        fuCapitalTransactionVO.setTxid(txid);
        fuCapitalTransactionVO.setType(1);
        // 2 确认中
        fuCapitalTransactionVO.setIsSuccess(2);
        fuCapitalTransactionVO.setCreatedDate(new Date());
        fuCapitalTransactionVO.setDataStatus(1);
        // 数据库中没有相应的 txid 才允许插入
        Map<String, Object> params = new HashMap<>();
        params.put("txid", fuCapitalTransactionVO.getTxid());
        List<FuCapitalTransactionVO> fuCapitalTransactionVOS = this.transactionService.loadFuCapitalTransaction(params);
        if (fuCapitalTransactionVOS.isEmpty()) {
            this.transactionService.insertFuCapitalTransaction(fuCapitalTransactionVO);
            System.out.println("txid 插入成功:" + txid);
        }

    }


    public static void main(String[] args) {
        String message = "34,123,92,34,104,97,115,104,92,34,58,92,34,48,120,49,99,57,98,102,49,51,100,50,52,51,99,51,56,102,52,102,97,97,102,99,56,57,56,49,57,102,101,99,100,51,55,48,54,99,100,100,101,52,100,53,99,51,57,53,49,52,100,98,100,48,97,55,48,54,101,100,55,98,50,57,51,57,97,92,34,44,92,34,116,111,92,34,58,92,34,48,120,99,98,54,57,101,99,100,101,54,98,97,100,98,53,49,48,55,56,48,56,102,51,54,101,54,56,48,99,54,52,98,100,53,98,97,98,101,54,102,51,92,34,44,92,34,105,110,112,117,116,92,34,58,92,34,48,120,92,34,44,92,34,118,97,108,117,101,92,34,58,92,34,51,48,48,48,48,48,48,48,48,48,48,48,48,48,48,48,48,48,48,92,34,44,92,34,103,97,115,92,34,58,51,49,52,49,53,48,125,34";
        String[] split = message.split(",");
        StringBuilder builder = new StringBuilder();
        for (int i = 0; i < split.length; i++) {
            builder.append((char) Integer.parseInt(split[i]));
        }
        message = builder.toString();
        System.out.println(message);
        message = message.replace("\\", "");
        System.out.println(message);
        message = message.substring(1, message.length() - 1);
        System.out.println(message);

    }

    @Autowired
    private IFuCapitalTransactionService transactionService;
}

